<template>
  <div>
    <el-collapse v-model="activeNames">
      <el-collapse-item name="1">
        <template slot="title">
          <span class="collapseTitle">链接属性</span>
        </template>
        <el-form :model="form" label-width="80px" label-position="left" size="medium">
          <el-form-item label="起始节点">
            <el-input v-model="form.source" readonly></el-input>
          </el-form-item>
          <el-form-item label="终止节点">
            <el-input v-model="form.target" readonly></el-input>
          </el-form-item>
        </el-form>
      </el-collapse-item>

      <el-collapse-item name="2">
        <template slot="title">
          <span class="collapseTitle">节点样式</span>
        </template>
        <el-form :model="form" label-width="80px" label-position="left">
          <el-form-item label="线路颜色">
            <el-color-picker show-alpha v-model="form.stroke" @change="handleFormChange"></el-color-picker>
          </el-form-item>
          
          <el-form-item label="线路宽度">
            <el-input-number v-model="form['stroke-width']" size="small" controls-position="right" @change="handleFormChange"></el-input-number>
          </el-form-item>
        </el-form>
      </el-collapse-item>

      <el-collapse-item name="3">
        <template slot="title">
          <span class="collapseTitle">自定义属性</span>
        </template>
        <el-form label-width="80px" label-position="left">
          <el-form-item v-for="(value, key) in edgeData" :key="key" :label="key">
            <el-input :value="value" readonly></el-input>
          </el-form-item>
        </el-form>
      </el-collapse-item>
    </el-collapse>
  </div>
</template>

<script>
var _ = require('lodash')
import GraphUtil from '@/utils/graph-utils/graphUtil'

export default {
  name: 'EdgePropertyPanel',
  data() {
    return {
      activeNames: ['1', '2', '3'],
      form: {
        stroke: '',
        'stroke-width': 2
      },
      edge: null,
      edgeData: {}
    }
  },
  watch: {
    '$store.state.graphModule.refreshNodeProp': function() {
      this.refreshPanel(this.$store.state.graphModule.operateNode)
    },

    '$store.state.graphModule.operateNode': function(newVal) {
      if (newVal && newVal.isEdge()) {
        this.refreshPanel(this.$store.state.graphModule.operateNode)
      }
    }
  },
  methods: {
    refreshPanel(edge) {
      if (!edge) {
        const graph = this.$store.state.graphModule.graph
        const selectedCells = graph.getSelectedCells()
        if (selectedCells.length === 0) {
          return
        }
        edge = selectedCells[0]
      }
      this.edge = edge

      // 链接属性
      const source = this.edge.getSource()
      if (source && source.cell) {
        this.form.source = source.cell
      }
      const target = this.edge.getTarget()
      if (target && target.cell) {
        this.form.target = target.cell
      }

      // 样式属性
      const edgeAttr = this.edge.getAttrs()
      const style = _.get(edgeAttr, 'line.style', '')
      const objStyle = GraphUtil.formatStrStyle2ObjStyle(style)
      this.form.stroke = _.get(objStyle, 'stroke', '')
      this.form['stroke-width'] = _.get(objStyle, 'stroke-width', 0)

      // 自定义属性
      this.edgeData = this.edge.getData()
    },

    handleFormChange() {
      if (this.edge) {
        const objStyle = {
          fill: 'none',
          stroke: this.form.stroke,
          'stroke-width': this.form['stroke-width']
        }
        const strStyle = GraphUtil.formatObjStyle2StrStyle(objStyle)
        this.edge.setAttrs({
          line: {
            style: strStyle
          }
        })
      }
    }
  }
}
</script>

<style scoped>
.el-input-number {
  width: 100px;
  line-height: 26px;
}

.el-form-item {
  margin-bottom: 2px;
}

.el-collapse {
  border: 3px solid #ebeef5;
  padding: 5px;
}

.collapseTitle {
  font-weight: bold;
  font-size: 1.2em;
}
</style>